#!/bin/sh
CUT=/mnt/card/cut

vafname="va1809.o"
version="1.08"
kernelVer="2.00"

echo "//////////////////////////////////////////////"
echo "SetUp VA1809A"
echo "//////////////////////////////////////////////"

function writeFPGA()
{
	#
	#--- 読込み専用にする ---
	#
	mount -o remount,ro /mnt/flash3

	#
	#--- FPGAを更新する ---
	#
	if [ -s /mnt/card/fpgawriter.o ]
	then
		echo " Update FPGA"
		/mnt/card/dispout.o 1 " FPGA WRITING .....     "
		/mnt/card/fpgawriter.o
	#	echo "Return Code : " $?
		case $? in
			255)/mnt/card/dispout.o 1 " Warnig FPGA WRITE ERR!!"
				/astro/testvram buzzer 4
				exit
				;;
			254)/mnt/card/dispout.o 1 " Warnig FPGA NMAP ERR!!"
				/astro/testvram buzzer 4
				exit
				;;
			253)/mnt/card/dispout.o 1 " Missing Device file !!"
				/astro/testvram buzzer 4
				exit
				;;
			252)/mnt/card/dispout.o 1 " Missing h8main.mot !!"
				/astro/testvram buzzer 4
				exit
				;;
			251)/mnt/card/dispout.o 1 " Missing fpgawriter.o !!"
				/astro/testvram buzzer 4
				exit
				;;
		esac
	fi
}


#
#--- 同一機種か調べ、違っていたら終了する ---
#
if [ ! -s /astro/va.o ]
then
	echo "Not VA-1809A !!"
	exit
fi


#
#--- プロセスの終了を待つ ---
#
echo "Wait Process End ..."
PID=`ps ax | grep va.o | grep -v grep | ${CUT} -f 4 -d " "`
echo "pid :"$PID
while [ "$PID" != "" ]
do
	echo " Wait for va.o.(1 seconds) ....."
	kill -9 $PID
	sleep 1
	PID=`ps ax | grep va.o | grep -v grep | ${CUT} -f 4 -d " "`
	echo "pid :"$PID
done
sleep 2

/mnt/card/dispout_va.o 0 $version

sleep 1



#
#--- PID Fileの削除 ---
#
echo "Delete Process File ..."
if [ -s /var/run/va.pid ]
then
	rm /var/run/va.pid
fi
#if [ -s /var/run/udpproc.pid ]
#then
#	rm /var/run/udpproc.pid
#fi

#
#--- 書込み可能にする ---
#
echo " Remount r/w mode ....."
mount -o remount,rw /mnt/flash3



#--------------------------------------
#2007.3.30追加n-kogure
#--- IPLをチェック
#
if [ -s /mnt/card/sh-stub.bin ]
then
	if [ -s /mnt/card/newipl2.dd ]
	then
		diff /dev/mtd1 /mnt/card/newipl2.dd
		if [ "$?" = "1" ]
		then
#--- IPLパーティションを削除 ---
#
			echo " eraseall /dev/mtd1"
			eraseall /dev/mtd1

#
#--- IPLを書き込む ---
#
			echo " cp -f /mnt/card/sh-stub.bin /dev/mtd1"
			cp -f /mnt/card/sh-stub.bin /dev/mtd1
		else
			echo " ipl same data"
		fi
	fi
fi
#--------------------------------------


#
#--- 最初にファイルを削除する ---
#
echo "Delete va.o ..."
cd /astro
for fname in va*.o
do
	rm $fname
	echo " remove "$fname
done
cd

#
#--- 確実にファイルを削除する ---
#
sync
mount -o remount,ro /mnt/flash3
sync
mount -o remount,rw /mnt/flash3
sync

/mnt/card/dispout_va.o 1 $version



#
#--- va1809.oを更新する ---
#
if [ -s /mnt/card/$vafname ]
then
	cp -f /mnt/card/$vafname /astro
	echo " Update "$vafname" ....."
fi
/mnt/card/dispout_va.o 2 $version

#
#--- ドライバファイルを更新する ---
#
if [ -s /mnt/card/va1809_drv.o ]
then
	echo " Update "va1809_drv.o" ..."
	cp -f /mnt/card/va1809_drv.o /astro
fi

if [ -s /mnt/card/va1809_lcdif.o ]
then
	echo " Update "va1809_lcdif.o" ..."
	cp -f /mnt/card/va1809_lcdif.o /astro
fi

if [ -s /mnt/card/va1809_frsw.o ]
then
	echo " Update "va1809_frsw.o" ..."
	cp -f /mnt/card/va1809_frsw.o /astro
fi


#if [ -s /mnt/card/udpproc ]
#then
#	echo " Update "udpproc" ..."
#    cp -f /mnt/card/udpproc /astro
#fi
#
#--- カーネルを更新する(Versionが異なる場合のみ) ---
#
/mnt/card/dispout_va.o 3 $version

#現在のカーネルバージョン読み込み
VER=`cat /proc/version | grep VA-1809 | /mnt/card/cut -f 2 -d " "`
if [ "$kernelVer" != "$VER" ]
then
	if [ -s /mnt/card/zImage ]
	then
		echo " Update zImage"

		eraseall /dev/mtd2
		cp -f /mnt/card/zImage /dev/mtd2
		echo "OLD kenel ver:"$VER
		echo "NEW kenel ver:"$kernelVer
	fi
else
	echo "Kenel is same!! can't update zImage "
fi

if [ -s /mnt/card/motd ]
then
	echo " Update "motd" ..."
	cp -f /mnt/card/motd /etc
fi

if [ -s /mnt/card/rc.local ]
then
	echo " Update "rc.local" ..."
	cp -f /mnt/card/rc.local /etc/rc.d
fi

#
#--- リンクを作成する ---
#
ln -s /astro/$vafname /astro/va.o
#
#---ライセンス書き込み ---
#
	cp -f /mnt/card/license.dat /astro
	echo " Update "license.dat" ....."
#
#--- FPGAを更新する ---
#
/mnt/card/dispout_va.o 4 $version

writeFPGA

sync
sync
sync

sleep 2

echo "//////////////////////////////////////////////"
echo "SetUp Complete."
sleep 2
echo "Please power off."
echo "//////////////////////////////////////////////"
/mnt/card/dispout_va.o 5 $version

exit 0

